<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>生成代码工具</title>

    <!--引入 element-ui 的样式，-->
    <link rel="stylesheet" href="/admin/plugin/element-ui/index.css">
    <!-- 必须先引入vue，  后使用element-ui -->
    <script type="text/javascript" src="/admin/js-css/js/vue.min.js"></script>
    <!-- 引入element 的组件库-->
    <script type="text/javascript" src="/admin/plugin/element-ui/index.js"></script>

    <script type="text/javascript" src="/admin/plugin/element-ui/httpVueLoader.js"></script>

    <!--引入layui组件-->
    <link rel="stylesheet" href="/admin/plugin/layui/dist/css/layui.css"  media="all">
    <script src="/admin/plugin/layui/dist/layui.js" charset="utf-8"></script>

    <script type="text/javascript" src="/admin/js-css/js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="/admin/js-css/js/mx1.0.js?v=1.1"></script>

    <style>

        .code_div div{
            line-height: 10px;
            height: 17px;
        }
    </style>
</head>
<body>
    <div id="app"  >
        <div style="padding: 16px 30px;">
            <el-form :inline="true"  class="demo-form-inline" size="small"  >
                <el-form-item label="保存对象引用名称：">
                    <el-input v-model="formInline.saveyy" placeholder="请输入保存对象引用名称"></el-input>
                </el-form-item>

                <el-form-item label="要复制数据对象引用名称：">
                    <el-input v-model="formInline.queryyy" placeholder="请输入要复制数据对象引用名称"></el-input>
                </el-form-item>
            </el-form>

            <el-form   class="demo-form-inline" size="small"  >
                <el-form-item label="要复制的表ID：">
                    <el-input v-model="formInline.copytableid" @input="getFieldColumns" style="width: 300px;" placeholder="请输入要复制的表ID"></el-input>
                </el-form-item>

                <el-form-item label="要复制的字段：">
                    <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
                    <el-checkbox-group v-model="checkList">
                        <el-checkbox :label="item.col_name" v-for="(item,index) in checkboxList">{{item.show_name}}</el-checkbox>
                    </el-checkbox-group>
                </el-form-item>


                <el-form-item label="接口代码生成：">
                    <el-button type="text" @click="getAdminCode">admin自定义接口</el-button>
                    <el-button type="text" @click="getWebCode">web自定义接口</el-button>
                    <el-button type="text" @click="copyText(classCode)">复制代码</el-button><br/>
                    <div class="code_div">
                        <div v-for="(c,index) in classCode.split('\n')" v-if="c">{{c}} <el-button type="text" @click="copyText(c)">复制</el-button><br/></div>
                    </div>

                </el-form-item>

                <el-form-item label="保存数据代码：">
                    <el-button type="text" @click="copyText(codeObj.t1)">复制</el-button><br/>
                    <div class="code_div">
                        <div v-for="(c,index) in codeObj.t1.split('\n')" v-if="c">{{c}} <el-button type="text" @click="copyText(c)">复制</el-button><br/></div>
                    </div>

                </el-form-item>


                <el-form-item label="更新表数据代码：">
                    <el-button type="text" @click="copyText(codeObj.t4)">复制</el-button><br/>
                    <div class="code_div">
                        <div v-for="(c,index) in codeObj.t4.split('\n')" v-if="c">{{c}} <el-button type="text" @click="copyText(c)">复制</el-button><br/></div>
                    </div>

                </el-form-item>


                <el-form-item label="查询表数据代码：">
                    <el-button type="text" @click="copyText(codeObj.t2)">复制</el-button><br/>
                    <div class="code_div">
                        <div v-for="(c,index) in codeObj.t2.split('\n')" v-if="c">{{c}} <el-button type="text" @click="copyText(c)">复制</el-button><br/></div>
                    </div>

                </el-form-item>



                <el-form-item label="JSON对象代码：">
                    <el-button type="text" @click="copyText(codeObj.t3)">复制</el-button><br/>
                    <div  class="code_div" >
                        <div v-for="(c,index) in codeObj.t3.split('\n')" v-if="c">{{c}} <el-button type="text" @click="copyText(c)">复制</el-button></div>
                    </div>

                </el-form-item>
            </el-form>
        </div>
    </div>
</body>
<script>

    var vm = new Vue({
        el:"#app",
        data:{
            formInline:{},
            checkList:[],
            checkListAll:[],
            checkboxList:[],
            checkAll: true,
            isIndeterminate: false,
            codeObj:{},
            parmsData :{},
            classCode:""
        },
        created(){
            this.parmsData = getParams();
            this.formInline.zygl_id = this.parmsData.zygl_id;
            this.formInline.ids = this.parmsData.ids;
            this.submitData();
        },
        methods:{
            async getAdminCode(){
                const res = await post("/admin/api/FieldManagementController/CodeGenerationAdmin",{zygl_id:this.parmsData.zygl_id});
                this.classCode = res.data;
            },
            async getWebCode(){
                const res = await post("/admin/api/FieldManagementController/CodeGenerationWeb",{zygl_id:this.parmsData.zygl_id});
                this.classCode = res.data;
            },
            async submitData(){
                this.formInline.checkList = this.checkList;
                const res = await postBody("/admin/api/FieldManagementController/CodeGenerationEvent2",this.formInline);
                this.codeObj = res.data;
            },
            handleCheckAllChange(val) {
                this.checkList = val ? this.checkListAll : [];
                this.isIndeterminate = false;
            },
            async getFieldColumns(v){
                const res = await post("/admin/api/FieldManagementController/getListColumns",{zygl_id:getTableId(v)});
                this.checkboxList = res.data;
                this.checkList = [];
                this.checkListAll = [];
                for(let a=0;a<this.checkboxList.length;a++){
                    this.checkList.push(this.checkboxList[a].col_name);
                    this.checkListAll.push(this.checkboxList[a].col_name);
                }
            },
            copyText(text) {
                var element = this.createElement(text);
                element.select();
                element.setSelectionRange(0, element.value.length);
                document.execCommand('copy');
                element.remove();
                top.alert("已复制到剪切板");
            },
            createElement(text) {
                var isRTL = document.documentElement.getAttribute('dir') === 'rtl';
                var element = document.createElement('textarea');
                // 防止在ios中产生缩放效果
                element.style.fontSize = '12pt';
                // 重置盒模型
                element.style.border = '0';
                element.style.padding = '0';
                element.style.margin = '0';
                // 将元素移到屏幕外
                element.style.position = 'absolute';
                element.style[isRTL ? 'right' : 'left'] = '-9999px';
                // 移动元素到页面底部
                let yPosition = window.pageYOffset || document.documentElement.scrollTop;
                element.style.top = `${yPosition}px`;
                //设置元素只读
                element.setAttribute('readonly', '');
                element.value = text;
                document.body.appendChild(element);
                return element;
            }
        }
    });
</script>
</html>